package com.yeraphael.controller;


import com.yeraphael.entity.PageBeandf;
import com.yeraphael.entity.Result;
import com.yeraphael.entity.Student;
import com.yeraphael.entity.StudentQueryParam;
import com.yeraphael.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result getStudents(StudentQueryParam param) {
        log.info("studentQueryParam: {}", param);
        PageBeandf pageBean = studentService.getStudents(param);
        return Result.success(pageBean);
    }

    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        log.info("student: {}", student);
        studentService.addStudent(student);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result getStudent(@PathVariable Integer id) {
        log.info("studentId: {}", id);
        Student student = studentService.getStudent(id);
        return Result.success(student);
    }

    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        log.info("student: {}", student);
        studentService.updateStudent(student);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable List<Integer> ids) {
        log.info("studentIds: {}", ids);
        studentService.deleteStudent(ids);
        return Result.success();
    }

    @PutMapping("violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Short score) {
        log.info("studentId: {}, score: {}", id, score);
        studentService.violation(id, score);
        return Result.success();
    }

}
